package com.bzkj.basicinfo.service.check.impl;

import com.bzkj.basicinfo.domain.check.WmsCheckTaskEntity;
import com.bzkj.basicinfo.mapper.check.WmsCheckDetailMapper;
import com.bzkj.basicinfo.mapper.check.WmsCheckTaskMapper;
import com.bzkj.common.utils.PageUtils;
import com.bzkj.common.core.page.TableDataInfo;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.basicinfo.service.check.WmsCheckTaskService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * WMS复核任务Service实现
 *
 * @author system
 * @date 2025-10-22
 */
@Service
public class WmsCheckTaskServiceImpl implements WmsCheckTaskService {

    @Autowired
    private WmsCheckTaskMapper checkTaskMapper;

    @Autowired
    private WmsCheckDetailMapper checkDetailMapper;

    @Override
    public TableDataInfo getCheckTaskList(WmsCheckTaskEntity entity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WmsCheckTaskEntity> list = checkTaskMapper.selectCheckTaskList(entity);
        PageInfo<WmsCheckTaskEntity> pageInfo = new PageInfo<>(list);
        return PageUtils.getTableDataInfo(pageInfo);
    }

    @Override
    public WmsCheckTaskEntity getCheckTaskDetail(Long id) {
        return checkTaskMapper.selectCheckTaskById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int createCheckTask(Long outboundOrderId, String checkType) {
        // 生成复核任务号：CHK + YYYYMMDDHHMMSS
        String taskNo = "CHK" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        WmsCheckTaskEntity entity = new WmsCheckTaskEntity();
        entity.setTaskNo(taskNo);
        entity.setOutboundOrderId(outboundOrderId);
        entity.setCheckType(checkType != null ? checkType : "full");
        entity.setTaskStatus("pending");
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(new Date());

        // TODO: 根据出库单创建复核明细

        return checkTaskMapper.insertCheckTask(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int assignCheckTask(Long id, String assignedUser) {
        WmsCheckTaskEntity entity = new WmsCheckTaskEntity();
        entity.setId(id);
        entity.setTaskStatus("assigned");
        entity.setAssignedUser(assignedUser);
        entity.setAssignTime(new Date());
        return checkTaskMapper.updateCheckTask(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int completeCheckTask(Long id) {
        WmsCheckTaskEntity entity = new WmsCheckTaskEntity();
        entity.setId(id);
        entity.setTaskStatus("completed");
        entity.setCompleteTime(new Date());
        return checkTaskMapper.updateCheckTask(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteCheckTask(Long id) {
        checkDetailMapper.deleteDetailsByTaskId(id);
        return checkTaskMapper.deleteCheckTaskById(id);
    }
}
